/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.barang;

import org.sadhar.sia.framework.ClassApplicationModule;
import org.sadhar.sipp.perekamananggarankegiatanunit.ItemBiayaBarang;
import org.sadhar.sipp.perekamananggarankegiatanunit.PerekamanAnggaranKegiatanUnitDAO;
import org.sadhar.sipp.perekamananggarankegiatanunit.PerekamanAnggaranKegiatanUnitDAOImpl;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author hendro
 */
public class BarangPopupWnd extends ClassApplicationModule {

    private Textbox txtboxKeySearch;
    private Textbox txtboxIdJenisBarang;
    private Listbox lstboxBarang;
    private Textbox txtboxTahunAnggaran;
    private BarangDAO bDao;
    private PerekamanAnggaranKegiatanUnitDAO unitDAO;

    public BarangPopupWnd() {
        bDao = new BarangDAOImpl();
        unitDAO = new PerekamanAnggaranKegiatanUnitDAOImpl();
    }

    public void onCreate() throws Exception {
        txtboxKeySearch = (Textbox) getFellow("txtboxKeySearch");
        txtboxIdJenisBarang = (Textbox) getFellow("txtboxIdJenisBarang");
        lstboxBarang = (Listbox) getFellow("lstboxBarang");
        txtboxTahunAnggaran = (Textbox) getFellow("txtboxTahunAnggaran");
        loadBarang();
    }

    private void loadBarang() throws Exception {
        lstboxBarang.getItems().clear();
        for (Barang brg : bDao.list(Integer.valueOf(txtboxIdJenisBarang.getValue()), txtboxKeySearch.getValue())) {
            Listitem item = new Listitem();
            item.setValue(brg);
            item.appendChild(new Listcell(brg.getNamaBarang()));
            lstboxBarang.appendChild(item);
        }
    }

    public void lstboxBarangOnSelect() throws Exception {
        Barang brg = (Barang) lstboxBarang.getSelectedItem().getValue();
        //System.out.println(brg.getIdBarang());
        ItemBiayaBarang itemBrg = unitDAO.selectItemBiayaBarang(brg.getIdBarang());
        Window window = (Window) this.getRoot().getFellow("contentDiv").getFirstChild();
        Label lblItemBiaya = (Label) window.getFellow("lblItemBiaya");
        lblItemBiaya.setValue(itemBrg.getNamaBarang());
        Label lblSatuan = (Label) window.getFellow("lblSatuan");
        lblSatuan.setValue(itemBrg.getSatuan());
        Textbox txtboxKodeItemBiayaOrIdHargaBarang = (Textbox)window.getFellow("txtboxKodeItemBiayaOrIdHargaBarang");
        txtboxKodeItemBiayaOrIdHargaBarang.setValue(brg.getIdBarang()+"");
        Doublebox txtboxHarga = (Doublebox) window.getFellow("txtboxHarga");
        txtboxHarga.setValue(unitDAO.selectHarga(txtboxTahunAnggaran.getValue(), brg.getIdBarang()));
        txtboxHarga.setDisabled(true);
        this.detach();
    }
}
